﻿//http://en.wikipedia.org/wiki/XMLHttpRequest
if (typeof XMLHttpRequest == "undefined")
    XMLHttpRequest = function () {
        try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
        catch (e) { }
        try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
        catch (e) { }
        try { return new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (e) { }
        //Microsoft.XMLHTTP points to Msxml2.XMLHTTP and is redundant
        throw new Error("This browser does not support XMLHttpRequest.");
    };

var lastFrom = 0;
var lastTo = 0;
var lineColourToggle = 0;

function getRoutes(from, to) {
    var xmlHttp = new XMLHttpRequest();

    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4) {

            var serial
            var ppp = xmlHttp.responseText;

            lineColourToggle = 0;
            var buffer = "<div class='brrowlight'>";

            try {
                var jsObject = JSON.parse(ppp, function (key, value) { return value; });
                var row;
                for (row in jsObject) {

                    var line = jsObject[row];
                    if (line == "") {
                        lineColourToggle++;
                        if (lineColourToggle % 2 == 0) {
                            buffer += "</div><div class='brrowlight'>";
                        }
                        else {
                            buffer += "</div><div class='brrowdark'>";
                        }
                    }
                    else {
                        buffer += "<p>";
                        buffer += line;
                        buffer += "</p><br />";
                    }
                }
            }
            catch (e) {
                alert("response text " + e.Message);
            }

            buffer += "</div>";

            // document.getElementById("time").innerText = buffer;
            //alert(buffer);
            document.getElementById("route_report").innerHTML = buffer;
        }
    }

    if ((lastFrom == from) && (lastTo == to)) {
        //document.getElementById("route_report").innerHTML = "<p>No Change " + lastFrom + " " + from + "</p>";
        return;
    }

    lastFrom = from;
    lastTo = to;

    var gif = "<div><img alt=''src='../images/spinner10.gif' /></div>";
    document.getElementById("route_report").innerHTML = gif;
    xmlHttp.open("GET", "../jsonbusroute.aspx?from=" + from + '&' + "to=" + to, true);
    xmlHttp.send(null);
}

function reportMatrix() {

    var dropdownFrom = document.getElementById("DropDownListFrom");
    var myindexFrom = dropdownFrom.selectedIndex;
    var SelValueFrom = dropdownFrom.options[myindexFrom].value;

    var dropdownTo = document.getElementById("DropDownListTo");
    var myindexTo = dropdownTo.selectedIndex;
    var SelValueTo = dropdownTo.options[myindexTo].value;

    if (SelValueTo == SelValueFrom) {
        document.getElementById("route_report").innerHTML = "<p>From and To are the same!</p>";
        lastFrom = SelValueFrom;
        lastTo = SelValueTo;
        return;
    }

    getRoutes(SelValueFrom, SelValueTo);
}       